{% extends "base.html" %}
{% load staticfiles %}

{% block title %}移交审批{% endblock %}

{% block content %}
    <!-- 移交审批确认 -->
    <div class="modal fade" data-backdrop="static" id="modal_trans_confirm">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="opr_title">审批确认</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <div style="margin-top:3%"><label>原归属人</label>
                            <input type="text" class="opts conf" id="preuser" readonly>
                        </div>
                        <div style="margin-top:3%"><label>策略组</label>
                            <input type="text" class="opts conf" id="group" readonly>
                        </div>
                        <div style="margin-top:3%"><label for="transferst">移交策略</label>
                            <input type="text" class="opts conf" id="transferst" readonly>
                        </div>
                        <div style="margin-top:3%"><label>移交至</label>
                            <input type="text" class="opts conf" id="nextuser" readonly>
                        </div>
                        <hr/>
                        <div style="height: 30px; display: block;margin-top: 20px">
                            <div style="padding-left:100px; float: left;">
                                <span class="btn-cancel btn-stc" onclick="fade_confirm()">取消</span>
                            </div>
                            <div style="padding-left:5px; float: left;">
                            <span class="btn-confirm btn-stc"
                                  onclick="approval_stg()">确定</span>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!-- 操作成功模态框-->
    <div class="modal fade" data-backdrop="static" id="oper_success">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="success-title"></h4>
                </div>
                <div class="modal-body">
                    <div class="operat-success" id="msg_success">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Main content -->
    <body>
    <section>
        <div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                <li class="active" style="margin-top:10px"><a href="#fa-pack" onclick="tabs(1)"
                                                              data-toggle="tab">移交审批</a></li>
            </ul>
            <div class="tab-content">
                <!--移交审批-->
                <div class="tab-pane active">
                    <div>
                        <div class="div-query">
                            <div class="lbl-query"><span>原归属人</span>
                                <input class="input-query" type="text" id="preowner">
                            </div>
                            <div class="lbl-query"><span>策略名称</span>
                                <input class="input-query" type="text" id="rulename">
                            </div>
                            <div class="lbl-query">
                                <span>策略状态</span>
                                <select class="input-query" id="rulestatus">
                                    <option value=""></option>
                                    <option value="1">待审批</option>
                                    <option value="3">审批通过</option>
                                </select></div>
                            <div class="lbl-query">
                                <button class="btn-query" onclick="trans_search()">查询</button>
                            </div>
                        </div>
                        <table class="table-stc" id="TransAppTable">
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </section>
    </body>
{% endblock %}

{% block scripts %}

    <script type="text/javascript">

        //查询
        function trans_search() {
            var oriowner = $("#preowner").val();
            var stgname = $("#rulename").val();
            var status = $("#rulestatus").val()
            $.ajax({
                url: "/trade/transfer_application/",
                type: 'post',
                data: {
                    "oowner": oriowner,
                    "stgname": stgname,
                    "status": status,
                    "type": "search"
                },
                success: function (data) {
                    rendertable(data)
                }
            })
        }

        //columns
        var columns = [
            {
                field: 'stgroup',
                title: '策略组',
                visible: false,
            },
            {
                field: 'orignalowner',
                width: 100,
                title: '原归属人'
            }, {
                field: 'ruleid',
                width: 100,
                title: '移交策略ID'
            }, {
                field: 'stgname',
                width: 100,
                title: '移交策略名'
            }, {
                field: 'newowner',
                title: '移交至',
                width: 100,
            },
            {
                field: 'applydatetime',
                title: '申请时间',
                width: 100,
            }, {
                field: 'auditstatus',
                title: '审批状态',
                width: 100,
            }, {
                field: 'option',
                title: '操作',
                width: 100,
                formatter: function (value, row, index) {
                    if (row.auditstatus === "待审批") {
                        var approva = "<span onclick=\"_approval('" + value + "','" + row.stgname + "','" +
                            row.orignalowner + "','" + row.newowner +"','" + row.stgroup+
                            "')\" class='btn btn-primary btn-xs btn-flat btn_operation' data-toggle='modal' id='appro'><i class='fa fa-check-circle-o'></i> 审批</span>"
                    } else {
                        var approva = '';
                    }
                    return approva;


                }
            }, {
                field: 'approvaldatetime',
                title: '审批时间',
                width: 100,
            }
        ];

        //渲染页面
        function rendertable(data) {
            var objlist = JSON.parse(data);
            var objtable = [];
            for (let i = 0; i < objlist.length; i++) {
                objlist[i].fields["option"] = objlist[i].fields["ruleid"];
                objtable.push(objlist[i].fields)
            }
            $("#TransAppTable").bootstrapTable("destroy").bootstrapTable({
                height: 600,
                columns: columns,
                data: objtable,
                // height: 600,
                {#width: 100,#}
                //******前端分页设置****
                pagination: true,
                pageNumber: 1,
                pageSize: 20,
                pageList: [10, 20, 50, 100],
                paginationHAlign: "right",
                paginationDetailHAlign: "left"
                //******前端分页设置****
            })
        }

        //定义全局变量
        var rulename, preuser, newuser, ruleid, stgroup = ""

        //审批模态框展示
        function _approval(stgid, stgname, preowner, newowner, stgup) {
            $("input").val("")
            rulename = stgname;
            preuser = preowner;
            newuser = newowner;
            ruleid = stgid;
            stgroup = stgup;
            if (stgroup === "1"){
                $("#group").val("做市策略")
            } else if (stgroup === "2"){
                $("#group").val("CTA策略")
            }
            $("#preuser").val(preuser)
            $("#transferst").val(rulename)
            $("#nextuser").val(newuser)
            $("#modal_trans_confirm").modal("show")
        }

        //审批逻辑
        function approval_stg() {
            fade_confirm()
            loadon()
            $.ajax({
                url: "/trade/transfer_approval/",
                type: "post",
                data: {
                    "stgid": ruleid,
                    "preuser": preuser,
                    "newuser": newuser,
                    "type": "approval"
                },
                success: function (data) {
                    loadoff()
                    if (data) {
                        rendertable(data)
                        $("#success-title").text("审批")
                        $("#msg_success").text("策略移交审批成功！")
                        $("#oper_success").modal("show")
                        var tip = setTimeout(function () {
                            $("#oper_success").modal("hide")
                            clearTimeout(tip)
                        }, 2000)
                    } else {
                        $("#success-title").text("审批")
                        $("#msg_success").text("服务器报错！")
                        $("#oper_success").modal("show")
                        var tip = setTimeout(function () {
                            $("#oper_success").modal("hide")
                            clearTimeout(tip)
                        }, 2000)
                    }
                }
            })
        }


        //模态框取消
        function fade_confirm() {
            $("#modal_trans_confirm").modal("hide")
        }

        $(function () {
            trans_search()
        })


    </script>

{% endblock %}